package com.tao.com.xiaowen.module.me;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.tao.com.xiaowen.R;
import com.tao.com.xiaowen.config.upload.UploadUntils;

import java.io.File;
import java.io.IOException;

/**
 * Created by tao on 17-3-2.
 */

public class UserInfoActivity extends AppCompatActivity implements View.OnClickListener {
    private LinearLayout background;
    private TextView titleView;
    private SimpleDraweeView userImg;
    private ImageButton returnBtn;

    protected static final int CHOOSE_PICTURE = 0;
    protected static final int TAKE_PICTURE = 1;
    private static final int PHOTO_CLIP = 2;
    protected static Uri tempUri;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_me_user_info);

        initView();
    }

    private void initView() {
        titleView = (TextView) findViewById(R.id.title);
        userImg = (SimpleDraweeView) findViewById(R.id.user_picture);
        returnBtn = (ImageButton) findViewById(R.id.return_button);
        background = (LinearLayout) findViewById(R.id.background);

        titleView.setText("返回");
        background.setBackgroundResource(R.drawable.one_profile_bg1);

        returnBtn.setOnClickListener(this);
        userImg.setOnClickListener(this);

        userImg.setImageURI("http://img2.imgtn.bdimg.com/it/u=2474223988,1358834824&fm=23&gp=0.jpg");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.return_button:
                finish();
                break;
            case R.id.user_picture:
                try {
                    showChooseImageDialog();
                } catch (Exception e) {
                    Log.d("TAG", e.getMessage());
                }
                break;
        }
    }

    public void showChooseImageDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("设置头像");
        String[] items = {"相册选择图片", "拍照"};
        builder.setNegativeButton("取消", null);
        builder.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which) {
                    case CHOOSE_PICTURE:
                        Intent picIntent = new Intent(Intent.ACTION_PICK, null);
                        picIntent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                        startActivityForResult(picIntent, CHOOSE_PICTURE);
                        break;
                    case TAKE_PICTURE:
                        Intent takeIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        tempUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "image.jpg"));
                        takeIntent.putExtra(MediaStore.EXTRA_OUTPUT, tempUri);
                        startActivityForResult(takeIntent, TAKE_PICTURE);
                        break;
                }
            }
        });
        builder.create().show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case CHOOSE_PICTURE:
                if (data != null) {
                    Uri uri = data.getData();
//                    photoClip(uri);
                    Bitmap photo = null;
                    try {
                        photo = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    File file = UploadUntils.getHelper(this).saveFile(photo, Environment.getExternalStorageDirectory().toString(), "hand.jpg");
                    UploadUntils.getHelper(this).toUpLoadFile(file);
                    userImg.setImageURI(uri);
                }
                break;
            case TAKE_PICTURE:
                File file = new File(Environment.getExternalStorageDirectory()
                        + "/hand.jpg");//保存图片
                UploadUntils.getHelper(this).toUpLoadFile(file);
                userImg.setImageURI(tempUri);
                break;
        }
    }

    private void photoClip(Uri uri) {
        // 调用系统中自带的图片剪裁
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        // 下面这个crop=true是设置在开启的Intent中设置显示的VIEW可裁剪
        intent.putExtra("crop", "true");
        // aspectX aspectY 是宽高的比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        // outputX outputY 是裁剪图片宽高
        intent.putExtra("outputX", 150);
        intent.putExtra("outputY", 150);
        intent.putExtra("return-data", true);
        startActivityForResult(intent, PHOTO_CLIP);
    }
}
